<script setup lang="ts">
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { useI18n } from '@/hooks/web/useI18n'
import { ElButton, ElTag,ElRow,ElCol,ElTree } from 'element-plus'
import { Table } from '@/components/Table'
import { getTableListApi, delTableListApi } from '@/api/table'
import { useTable } from './vCircle/web/vCircleTable'
import {h, reactive, ref} from 'vue'
import { useRouter } from 'vue-router'
import { useEmitt } from '@/hooks/web/useEmitt'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { TableColumn } from '@/types/table'
import { getCaoZuoRiZhi , delectForum} from '@/views/QuanXian/vCircle/table'
import { inject } from "vue";


defineOptions({
  name: 'ExamplePage'
})

const { push } = useRouter()

const { register, tableObject, methods } = useTable<TableData>({
  getListApi: getCaoZuoRiZhi,
  delListApi: delTableListApi,
  response: {
    list: 'list',
    total: 'total'
  }
})

const { getList, setSearchParams } = methods

getList()

useEmitt({
  name: 'getList',
  callback: (type: string) => {
    if (type === 'add') {
      tableObject.currentPage = 1
    }
    getList()
  }
})

const { t } = useI18n()

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'id',
    label: "序号",
    type: 'id'
  },
  {
    field: 'trueName',
    label: "管理员名称"
  },
  {
    field: 'tablename',
    label: "操作列表"
  },
  {
    field: 'createDate',
    label: "执行时间",
  },
  {
    field: 'type',
    label: "操作类型",
    formatter: (_: Recordable, __: TableColumn, cellValue: number) => {
      return h(
          ElTag,
          {
            type: cellValue === 2 ? 'success' : cellValue === 3 ? 'warning' : 'danger'
          },
          () =>
              cellValue === 2
                  ? "添加"
                  : cellValue === 3
                      ? "删除"
                      : cellValue === 4
                          ? "修改"
                          : "查找"
      )
    }
  },
  {
    field: 'remark',
    label: "备注",
  },
  {
    field: 'action',
    label: "操作"
  },
/*  {
    field: 'articleLike',
    label: t('page.articleLike')
  },
  {
    field: 'articleHits',
    label: t('page.articleHits')
  },
  {
    field: 'articleReviews',
    label: t('page.articleReviews')
  },
  {
    field: 'articleShare',
    label: t('page.articleShare')
  },
  {
    field: 'action',
    width: '260px',
    label: t('tableDemo.action')
  }*/
])
/*//树
const handleNodeClick = async(e) => {
  const res = await getVcirce({actid:e.id})
  tableObject.tableList=res.data
}
let data=ref([{
  name:'请选择课程' ,
  children:[],
}]);

tree().then((res)=>{
  data.value[0].children=res.data
})

const defaultProps = {
  label: 'name',
  children: 'children',
}*/

const { allSchemas } = useCrudSchemas(crudSchemas)
//添加
const AddAction = () => {
  push('/example/example-add')
}
//批量删除
const delLoading = ref(false)

//局部刷新
const ceshi = inject("reload");
//行内删除
const delData = async (row: TableData | null, multiple: boolean) => {
  delectForum({"id" : row.id}).then((res) =>{
    if(res){
      getList()
    }
  })
}


//表单查询
import { Form } from '@/components/Form'
import {FormSchema} from "@/types/form";
import {TableData} from "@/api/table/types";

const schema = reactive<FormSchema[]>([
  {
    field: 'trueName',
    label: '管理员名称',
    component: 'Input'
  },
  {
    field: 'tablename',
    label: '操作的列表',
    component: 'Input'
  }
  ,{
    field: 'type',
    label: '操作类型',
    component: 'Select',
    componentProps:{
      options:[
        {
          label:"添加",
          value:"2"
        },
        {
          label:"删除",
          value:"3"
        },
        {
          label:"修改",
          value:"4"
        },
        {
          label:"查找",
          value:"5"
        }
      ]
    }
  },{
    field: 'createDate',
    label: '操作时间',
    component: 'Input'
  }
])

</script>

<template>

  <ElRow class="row-con">
<!--    <ElCol :xl="3" :lg="3" :md="24" :sm="24" :xs="24">
      <ElTree :data="data" :props="defaultProps" @current-change="handleNodeClick" default-expand-all="false"/>
    </ElCol>-->
    <ElCol :xl="24" :lg="24" :md="24" :sm="24" :xs="24">
      <ContentWrap>
        <!--<Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />-->
        <Search
                :expand = "true"
                :schema="schema"
                :expandField = str
                @search= "setSearchParams"
                @reset= "setSearchParams"
        />
        <!--<div class="mb-10px">
          <ElButton type="primary" @click="AddAction">{{ t('exampleDemo.add') }}</ElButton>
          <ElButton :loading="delLoading" type="danger" @click="delData(null, true)">
            {{ t('exampleDemo.del') }}
          </ElButton>
        </div>-->

        <Table
                v-model:pageSize="tableObject.pageSize"
                v-model:currentPage="tableObject.currentPage"
                :columns="allSchemas.tableColumns"
                :data="tableObject.tableList.slice((tableObject.currentPage-1) * tableObject.pageSize, tableObject.currentPage * tableObject.pageSize)"
                :loading="tableObject.loading"
                :pagination="{
        total: tableObject.total
      }"
                @register="register"
        >
          <template #action="{ row }">
<!--            <ElButton type="success" @click="action(row, 'detail')">
              {{ t('详情') }}
            </ElButton>-->
            <ElButton type="danger" @click="delData(row, false)">
              {{ t('删除') }}
            </ElButton>
          </template>
        </Table>
      </ContentWrap>
    </ElCol>
  </ElRow>


</template>
